﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using FiddlerFirePHPInspector.BusinessObjects.FirePHP;

namespace FiddlerFirePHPInspector.FiddlerExtension
{
    public partial class FirePHPInspectorUserControl : UserControl
    {
        private readonly Uri inspectorUri;

        public FirePHPInspectorUserControl()
        {
            InitializeComponent();

            firePHPDataGrid.CellFormatting += FirePHPDataGridCellFormatting;
            firePHPLogBindingSource.CurrentItemChanged += FirePHPLogBindingSourceCurrentItemChanged;

            string inspectorUriPath = string.Format(@"{0}\VariableInspectorPanel.htm", new FileInfo(Assembly.GetExecutingAssembly().Location).Directory);
            inspectorUri = new Uri(inspectorUriPath);

            firePHPWebBrowser.Url = inspectorUri;
        }

        void FirePHPLogBindingSourceCurrentItemChanged(object sender, EventArgs e)
        {
            firePHPWebBrowser.Visible = true;
            FirePHPLog result = (FirePHPLog)firePHPLogBindingSource.Current;

            if (result != null)
            {
                firePHPWebBrowser.Document.InvokeScript("CallRenderVariable", new object[] { string.Format("[{0}]", result.LogBody) });
            }
        }

        void FirePHPDataGridCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((firePHPDataGrid.Rows[e.RowIndex].DataBoundItem != null) && (firePHPDataGrid.Columns[e.ColumnIndex].DataPropertyName.Contains(".")))
            {
                e.Value = BindProperty(firePHPDataGrid.Rows[e.RowIndex].DataBoundItem, firePHPDataGrid.Columns[e.ColumnIndex].DataPropertyName);
            }
        }

        public List<FirePHPLog> FirePHPLogs
        {
            set
            {
                firePHPLogBindingSource.Clear();
                firePHPLogBindingSource.DataSource = value;
                if (value == null)
                {
                    firePHPWebBrowser.Visible = false;
                }
            }
        }

        public void Clear()
        {
            firePHPWebBrowser.Url = inspectorUri;
        }

        private static string BindProperty(object property, string propertyName)
        {
            string retValue = null;

            if (propertyName.Contains("."))
            {
                string leftPropertyName = propertyName.Substring(0, propertyName.IndexOf("."));
                PropertyInfo[] arrayProperties = property.GetType().GetProperties();

                foreach (PropertyInfo propertyInfo in arrayProperties)
                {
                    if (propertyInfo.Name == leftPropertyName)
                    {
                        retValue = BindProperty(propertyInfo.GetValue(property, null), propertyName.Substring(propertyName.IndexOf(".") + 1));
                        break;
                    }
                }
            }
            else
            {
                Type propertyType = property.GetType();
                PropertyInfo propertyInfo = propertyType.GetProperty(propertyName);

                object value = propertyInfo.GetValue(property, null);
                if (value != null)
                {
                    retValue = value.ToString();
                }
            }

            return retValue;
        }

    }
}


